package cn.stylefeng.guns.modular.comm.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.comm.entity.Country;
import cn.stylefeng.guns.modular.comm.mapper.CountryMapper;
import cn.stylefeng.guns.modular.comm.model.params.CountryParam;
import cn.stylefeng.guns.modular.comm.model.result.CountryResult;
import cn.stylefeng.guns.modular.comm.service.CountryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 洲和国家分类 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-25
 */
@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements CountryService {

    @Override
    public void add(CountryParam param) {
        Country entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CountryParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CountryParam param) {
        Country oldEntity = getOldEntity(param);
        Country newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CountryResult findBySpec(CountryParam param) {
        return null;
    }

    @Override
    public List<CountryResult> findListBySpec(CountryParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CountryParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<CountryResult> findAllCountryOnly() {
        return this.baseMapper.findAllCountryOnly();
    }

    private Serializable getKey(CountryParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Country getOldEntity(CountryParam param) {
        return this.getById(getKey(param));
    }

    private Country getEntity(CountryParam param) {
        Country entity = new Country();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
